<?php

function detail_info($id){

 	$fields = my_get_data_by_id('spd','spd_id',$id);		
	$keperluan = my_get_data_by_id('spd_keperluan','keperluan_id',$fields['keperluan_id']);

	$query = "SELECT * FROM spd_detail  a
	INNER JOIN spd_biaya b ON a.biaya_id = b.biaya_id
	WHERE a.spd_id ={$id}
	";
	$result = my_query($query);
	$row = array();
	$i=0;
	while( $ey = my_fetch_array($result) ){
		$i++; 
		if( $fields['tipe_karyawan'] == 'Karyawan' ){
			$nominal = get_golongan_jabatan_karyawan($fields['karyawan_id'], $ey['biaya_id']);
		}else{
			$nominal =get_nominal_non_karyawan($fields['karyawan_id'] , $ey['biaya_id']);
		}
		
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['spd_detail_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$detail = spd_detail_data($id , $ey['biaya_id']);
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'jenis'=>  $ey['label_biaya'] ,   
			'tarif'=> position_text_align( 'Rp. '.rupiah_format($nominal) ,  'right'), 
			'jumlah'=> position_text_align( $detail['jumlah'],'center'),
			'total'=>position_text_align( 'Rp. '.rupiah_format($detail['total']) , 'right'),  
			'operasi'=> position_text_align(  $edit_button  , 'right'),  
	
		);
	}
	$header = array(
		'#'		=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Jenis Biaya'	=>array('style'=>'border-bottom:2px solid;width:25%'),   
		'Tarif'=>array('style'=>'text-align:center;border-bottom:2px solid;width:20%'),  
		'Jumlah'=>array('style'=>'text-align:center;border-bottom:2px solid;width:20%'),  
		'Total'=>array('style'=>'text-align:center;border-bottom:2px solid;width:20%'),  
		 	' '			=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 

	);
	
	$datas = table_rows($row);
	$view = form_header( "devisi" , "devisi"  );
	$view .= form_field_display( $fields['nomor']   , "Nomor"   );
	$view .= form_field_display( date('d - m - Y' , strtotime($fields['tanggal_mulai'] )).' s/d '.   
		date('d - m - Y' , strtotime($fields['tanggal_selesai'] )), "Tanggal"   );
	$view .= form_field_display( $fields['kota']   , " Kota"   );
	$view .= form_field_display( $keperluan['label_keperluan']   , " Keperluan"   );
	$view .= form_field_display( $fields['kendaraan']   , " Kendaraan"   );
		if(trim($fields['nomor_polisi']) <> '')
	$view .= form_field_display( $fields['nomor_polisi']   , " Nomor Polisi"   );	
	$view .= form_footer( );
	if( $fields['tipe_karyawan'] == 'Karyawan' ){
		$menuontop = array(  
			'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=spd&task=karyawan\''), 
		);
	}else{
		$menuontop = array(  
			'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=spd&task=non_karyawan\''), 
		);	
	}	
	$printmenu = additional_menu_on_list( $menuontop , '');
	return $view.$printmenu.table_builder($header , $datas , 7 , false );

}

function spd_detail_data($spd_id , $biaya_id){
	$query = "SELECT jumlah , total FROM spd_detail WHERE biaya_id = {$biaya_id} AND spd_id = {$spd_id} ";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row;
}

function get_nominal_non_karyawan($jenis_non_karyawan_id , $biaya_id){
	$query = "SELECT nominal FROM spd_jenis_non_karyawan_biaya a
	INNER JOIN spd_non_karyawan b ON a.jenis_non_karyawan_id = b.jenis_non_karyawan_id
	WHERE a.jenis_non_karyawan_id ={$jenis_non_karyawan_id}
	AND a.biaya_id = {$biaya_id}
	"; 
	$result = my_query($query);
	$rw = my_fetch_array($result);
	return $rw['nominal'];
	
}

function get_golongan_jabatan_karyawan($karyawan_id, $biaya_id){
	$query = "SELECT golongan_jabatan_id FROM kekaryawanan WHERE karyawan_id = {$karyawan_id}";
	$result = my_query($query);
	$rw = my_fetch_array($result);
	//return $rw['golongan_jabatan'];
 
	$query2 = "SELECT nominal FROM spd_biaya_golongan_jabatan WHERE golongan_jabatan_id ={$rw['golongan_jabatan_id']}
	AND biaya_id ={$biaya_id} ";
	  
		$result2 = my_query($query2);
	$rw2 = my_fetch_array($result2);
	return $rw2['nominal'];
}

function form_submit_biaya_detail($id = 0){
	$fields = my_get_data_by_id('spd_detail','spd_detail_id',$id);
	$spd = my_get_data_by_id('spd','spd_id',$fields['spd_id']);
	  
	if( $spd['tipe_karyawan'] == 'Karyawan' ){
		$nominal = get_golongan_jabatan_karyawan($spd['karyawan_id'], $fields['biaya_id']);
	}else{
		$nominal =get_nominal_non_karyawan($spd['karyawan_id'] , $fields['biaya_id']);
	}
	if(isset($_POST['jumlah'])){
		$jumlah = $_POST['jumlah'];
		$total = $_POST['jumlah'] * $nominal;
	}else{
		$jumlah = 0;
		$total = $_POST['total'];
	}
	
	$datas = array(
		'jumlah'=>my_type_data_int($jumlah),
		'total'=>my_type_data_int($total),
	);
	 
	return my_update_record('spd_detail','spd_detail_id',$id,$datas);
	
}

function form_biaya_detail_create($id=0){
	$view = form_header( "devisi" , "devisi"  );
 	
	$fields = my_get_data_by_id('spd_detail','spd_detail_id',$id);
	if($fields['status_tarif'] =='Tarif'){ 
	$jumlah = array(
				'name'=>'jumlah',
				'value'=>( isset($_POST['jumlah']) ? $_POST['jumlah'] : $fields['jumlah'] ),
				'id'=>'jumlah',
				'type'=>'text',
				'size'=>'10'
			);
	$form_field_jumlah = form_dynamic($jumlah);
	$view .= form_field_display( $form_field_jumlah  , "Jumlah" );
	 }else{
	$total = array(
				'name'=>'total',
				'value'=>( isset($_POST['total']) ? $_POST['total'] : $fields['total'] ),
				'id'=>'total',
				'type'=>'text',
				'size'=>'25'
			);
	$form_field_jumlah = form_dynamic($total);
	$view .= form_field_display( $form_field_jumlah  , "Total" );
	  
	 
	 }
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

